﻿using DevExpress.XtraEditors;
using DLL;
using NursingFormSystem;
using NursingFormSystem.BLL;
using NursingFormSystem.Gestation;
using NursingFormSystem.Model;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace NursingDocumentInfoSystem
{
    public partial class GestationForm : XtraForm
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        #region 窗体函数
        public GestationForm(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            //成员变量
            LoginUser = loginUser;
            PatientInfo = patientInfo;
            //绑定数据
            BindControl();
            BindInfo();
        }
        #endregion

        #region 查询公用方法
        /// <summary>
        /// 设置控件属性
        /// </summary>
        public void BindControl()
        {
            this.dateE_StartTime.DateTime = DateTime.Now.AddDays(-30).Date;
            this.dateE_EndTime.DateTime = DateTime.Now;
        }
        /// <summary>
        /// 依据日期区间查询
        /// </summary>
        /// <param name="dateE_StartTime"></param>
        /// <param name="dateE_EndTime"></param>
        public void BindInfo()
        {
            if (this.PatientInfo == null) return;
            string patientid = this.PatientInfo.PATIENT_ID;   //住院号
            int VisitId = this.PatientInfo.VISIT_ID;   //住院次数
            int FormId = (int)FormType.Fetation;    //表单ID
            DataTable dt = new DataTable();
            dt.Columns.Add("RiQi", typeof(string));
            dt.Columns.Add("ShiJian", typeof(string));   //时间
            dt.Columns.Add("XueYa_Zuo", typeof(string));   //血压_左
            dt.Columns.Add("XueYa_You", typeof(string));    //血压_右
            dt.Columns.Add("TaiDongJiShu", typeof(string));   //胎动计数
            dt.Columns.Add("TaiFangWei", typeof(string));    //胎方位
            dt.Columns.Add("TaiXianLu", typeof(string));    //胎先露
            dt.Columns.Add("TaiXinWeiZhi", typeof(string));    //胎心位置
            dt.Columns.Add("TaiXinCiShu", typeof(string));    //胎心次数
            dt.Columns.Add("GongSuoChiXu", typeof(string));   //宫缩持续
            dt.Columns.Add("GongSuoJianXie", typeof(string));   //宫缩间歇
            dt.Columns.Add("GongSuoQiangDu", typeof(string));    //宫缩强度
            dt.Columns.Add("GongKouWeiZhi", typeof(string));     //宫口位置
            dt.Columns.Add("GongKouZhiDi", typeof(string));  //宫口质地
            dt.Columns.Add("GongKouDaXiao", typeof(string));   //宫口大小
            dt.Columns.Add("XianLuGaoDi", typeof(string));   //先露高低
            dt.Columns.Add("TaiMo", typeof(string));    //胎膜
            dt.Columns.Add("CuiChanSuDianDiNongDu", typeof(string));    //催产素滴数浓度
            dt.Columns.Add("CuiChanSuDianDiDiShu", typeof(string));    //催产素滴数滴数
            dt.Columns.Add("QiTa", typeof(string));    //其他
            dt.Columns.Add("HuShiQiamMing", typeof(string));   //签名                     
            dt.Columns.Add("Id", typeof(string));
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, dateE_StartTime.DateTime, dateE_EndTime.DateTime);

            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    Pregnancy py = new Pregnancy();
                    py = XmlUtil.XMLToObject<Pregnancy>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["RiQi"] = Convert.ToDateTime(py.RiQi).ToString("yyyy-MM-dd");   //日期
                    newRow["ShiJian"] = DateTimeHelper.HHmmssToHHmm(py.ShiJian);   //时间
                    newRow["XueYa_Zuo"] = py.XueYa_Zuo;   //血压_左
                    newRow["XueYa_You"] = py.XueYa_You;    //血压_右
                    newRow["TaiDongJiShu"] = py.TaiDongJiShu;   //胎动计数
                    newRow["TaiFangWei"] = py.TaiFangWei;    //胎方位
                    newRow["TaiXianLu"] = py.TaiXianLu;    //胎先露
                    newRow["TaiXinWeiZhi"] = py.TaiXinWeiZhi;    //胎心位置
                    newRow["TaiXinCiShu"] = py.TaiXinCiShu;    //胎心次数
                    newRow["GongSuoChiXu"] = py.GongSuoChiXu;   //宫缩持续
                    newRow["GongSuoJianXie"] = py.GongSuoJianXie;   //宫缩间歇
                    newRow["GongSuoQiangDu"] = py.GongSuoQiangDu;    //宫缩强度
                    newRow["GongKouWeiZhi"] = py.GongKouWeiZhi;     //宫口位置
                    newRow["GongKouZhiDi"] = py.GongKouZhiDi;   //宫口质地
                    newRow["GongKouDaXiao"] = py.GongKouDaXiao;   //宫口大小
                    newRow["XianLuGaoDi"] = py.XianLuGaoDi;   //先露高低
                    newRow["TaiMo"] = py.TaiMo;    //胎膜
                    newRow["CuiChanSuDianDiNongDu"] = py.CuiChanSuNongDu;    //催产素滴数浓度
                    newRow["CuiChanSuDianDiDiShu"] = py.CuiChanSuDiShu;    //催产素滴数滴数
                    newRow["QiTa"] = py.QiTa;    //其他
                    newRow["HuShiQiamMing"] = py.QianMing;    //签名
                    //ID隐藏，双击触发事件
                    newRow["Id"] = record.Id;
                    dt.Rows.Add(newRow);
                }
            }
            dt.DefaultView.Sort = "RiQi DESC,ShiJian DESC";
            DataTable data = dt.DefaultView.ToTable();
            gridControl1.DataSource = data;
        }
        #endregion

        #region 查询
        private void btn_Query_Click(object sender, EventArgs e)
        {
            BindInfo();
        }

        #endregion

        #region 新增
        private void btn_Add_Click(object sender, EventArgs e)
        {
            GestationEntryForm gn = new GestationEntryForm(LoginUser, PatientInfo, string.Empty);
            gn.ShowDialog();
            if (gn.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 修改
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                GestationEntryForm gn = new GestationEntryForm(LoginUser, PatientInfo, Id);
                gn.ShowDialog();
                if (gn.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region 删除
        private void btn_Del_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) != DialogResult.OK) return;
            string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
            //取得数据信息海泰备用
            string sql = string.Format(" SELECT TOP 1 * FROM dbo.NursingFormRecord(NOLOCK) WHERE Id='{0}' ", id);
            DataSet ds = SqlHelper.ExecuteDataset(sql);
            DataRow row = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                row = ds.Tables[0].Rows[0];

            var ret2 = formService.DeleteNursingFormRecord(id);
            MessageBox.Show(ret2.M);
            if (ret2.Code != 1) return;

            string dateE_StartTime = this.dateE_StartTime.Text;
            string dateE_EndTime = this.dateE_EndTime.Text;
            BindInfo();
            #region 上传海泰
            if (Base.ESSwitchHT.Equals("1"))
            {
                if (row == null) return;
                Pregnancy pc = new Pregnancy();
                pc = XmlUtil.XMLToObject<Pregnancy>(row["NursingFormData"].ToString(), Encoding.UTF8);

                StringBuilder data = new StringBuilder();
                data.Append("<NursingRecordDocument>");
                data.Append("<Systolicpressure>").Append(pc.XueYa_Zuo).Append("</Systolicpressure>");
                data.Append("<Diastolicpressure>").Append(pc.XueYa_You).Append("</Diastolicpressure>");

                data.Append("<specialbilleles>");//(一条记录对应多个项目，specialbillele节点循环增加)

                data.Append("<specialbillele>");
                data.Append("<option>1</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.973</eleid>");
                data.Append("<elename>宫缩强度</elename>");//HT51.02.973	宫缩强度	下拉
                data.Append("<elevalue>").Append(pc.GongSuoQiangDu).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.976</eleid>");
                data.Append("<elename>宫口大小(cm)</elename>");//HT51.02.976	宫口大小	文本
                data.Append("<elevalue>").Append(pc.GongKouDaXiao).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.979</eleid>");
                data.Append("<elename>催产素点滴浓度</elename>");//HT51.02.979	催产素点滴浓度	文本
                data.Append("<elevalue>").Append(pc.CuiChanSuNongDu).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.980</eleid>");
                data.Append("<elename>催产素点滴滴数</elename>");//HT51.02.980	催产素点滴滴数	文本
                data.Append("<elevalue>").Append(pc.CuiChanSuDiShu).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.31.061</eleid>");
                data.Append("<elename>其他</elename>");//HT51.02.31.061	其他	多行文本
                data.Append("<elevalue>").Append(pc.QiTa).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.1008</eleid>");
                data.Append("<elename>宫缩持续</elename>");//HT51.02.1008	宫缩持续	文本
                data.Append("<elevalue>").Append(pc.GongSuoChiXu).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>PRESSURE</eleid>");
                data.Append("<elename>血压</elename>");//PRESSURE	血压	文本
                data.Append("<elevalue>").Append(pc.XueYa_Zuo).Append("/").Append(pc.XueYa_You).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>1</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.967</eleid>");
                data.Append("<elename>胎方位</elename>");//HT51.02.967	胎方位	下拉
                data.Append("<elevalue>").Append(pc.TaiFangWei).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>1</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.968</eleid>");
                data.Append("<elename>胎先露</elename>");//HT51.02.968	胎先露	下拉
                data.Append("<elevalue>").Append(pc.TaiXianLu).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>1</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.970</eleid>");
                data.Append("<elename>胎心位置</elename>");//HT51.02.970	胎心位置	下拉
                data.Append("<elevalue>").Append(pc.TaiXinWeiZhi).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.971</eleid>");
                data.Append("<elename>胎心次数</elename>");//HT51.02.971	胎心次数	文本
                data.Append("<elevalue>").Append(pc.TaiXinCiShu).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.1007</eleid>");
                data.Append("<elename>宫缩间隔</elename>");//HT51.02.1007	宫缩间隔	文本
                data.Append("<elevalue>").Append(pc.GongSuoJianXie).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.966</eleid>");
                data.Append("<elename>胎动计数</elename>");//HT51.02.966	胎动计数	文本
                data.Append("<elevalue>").Append(pc.TaiDongJiShu).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.975</eleid>");
                data.Append("<elename>宫口质地</elename>");//HT51.02.975	宫口质地	文本
                data.Append("<elevalue>").Append(pc.GongKouZhiDi).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.974</eleid>");
                data.Append("<elename>宫口位置</elename>");//HT51.02.974	宫口位置	文本
                data.Append("<elevalue>").Append(pc.GongKouWeiZhi).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>1</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.978</eleid>");
                data.Append("<elename>胎膜</elename>");//HT51.02.978	胎膜	下拉
                data.Append("<elevalue>").Append(pc.TaiMo).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("<specialbillele>");
                data.Append("<option>0</option>");//是否是下拉值（0为文本框，1为下拉值）
                data.Append("<eleid>HT51.02.977</eleid>");
                data.Append("<elename>先露高低</elename>");//HT51.02.977	先露高低	文本
                data.Append("<elevalue>").Append(pc.XianLuGaoDi).Append("</elevalue>");//元素值
                data.Append("</specialbillele>");

                data.Append("</specialbilleles> </NursingRecordDocument>");

                SynchroBLL synchro = new SynchroBLL();
                ClinicalNursinguser cn = new ClinicalNursinguser();
                cn.PatientId = row["PatientId"].ToString();
                cn.PatientName = row["PatientName"].ToString();
                cn.DepId = row["DepartmentId"].ToString();
                cn.DepName = row["DepartmentName"].ToString();
                cn.AreaId = row["AreaId"].ToString();
                cn.AreaName = row["AreaName"].ToString();
                cn.data = data.ToString();
                cn.Date = Convert.ToDateTime(row["AssessmentDate"]).ToString("yyyy-MM-dd");
                cn.Time = Convert.ToDateTime(row["AssessmentDate"]).ToString("HH:mm");
                cn.NursName = pc.QianMing;
                cn.VisitId = Convert.ToInt32(row["VisitId"]);
                cn.UpdateuserId = this.LoginUser.UserId;
                cn.UpdateuserName = this.LoginUser.UserName;
                string rst = pc.syncSN;
                cn.syncSN = rst;
                synchro.clinicalNursingInfoAsyncToEMRDelegateMethod(cn, "003", "3");
            }
            #endregion
        }
        #endregion

        #region 打印
        private void btn_PrintView_Click(object sender, EventArgs e)
        {
            GestationPrint gp = new GestationPrint(LoginUser, PatientInfo, dateE_StartTime.DateTime, dateE_EndTime.DateTime);
            gp.Show();
        }
        #endregion

        #region 产程图
        /// <summary>
        /// 产程图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region 双击事件
        private void bandedGridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                if (bandedGridView1.RowCount > 0)
                {
                    string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                    GestationEntryForm gn = new GestationEntryForm(LoginUser, PatientInfo, Id);
                    gn.ShowDialog();
                    if (gn.DialogResult == DialogResult.OK)
                    {
                        BindInfo();
                    }
                }
            }
        }
        #endregion
    }
}